<template>
	<view class="container">
		<!-- <view class="top">标题</view> -->
		<view class="middle">
			<view class="list">
				<view class="logo">
					<image src="../../static/logo.png" mode="aspectFit"></image>
				</view>
				<view class="name">申请获取以下权限</view>
				<text class="name">获得您的公开信息（昵称，头像等）</text>
			</view>
		</view>
		<view class="bottom">
			<button class="btn" @click="auth">授权</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		onShow() {
			// token
			// 注册（跳转授权页）
			// 登录（同上）
			// 失效（同上）
			let that = this;
			var obj = this.getRequest();
			var token = uni.getStorageSync('token');
			if(!obj['code']){
				obj['code'] = token;
				// uni.removeStorageSync('token');
			}
			if(obj['code']){
				let target = obj['target'];
				if(!target){
					let route = '/pages/my/my';
					let pages = getCurrentPages();
					if(pages.length >= 2 && pages[pages.length - 2].route != 'pages/my/auth'){
						route = pages[pages.length - 2].__page__.fullPath/* .fullPath */;
					}
					target = route;
				}
				// 校验 & 存储
				that.$http.httpReq({url:'api/member/index',token:obj['code'],data:{auth:true}})
					.then(res=>{
						console.log(res);
						console.log(target);
						if(res.data.code == 200){
							uni.setStorageSync('token',obj['code']);
							that.$store.commit('login',{token:obj['code']});
							return uni.reLaunch({
								url:target
							})
						}
						uni.removeStorageSync('token');
						this.auth();
					}).catch(err=>{
						console.log(err);
					});
			}
			
			// 登录成功 跳转我的页面（失败提示）
			// token 存在 请求用户信息 同步 store 跳转 || 不存在 请求登录授权 请求用户信息 同步 store 跳转
			// that.$http.httpReq({url:'api/member/index',token:'',data:{auth:true}})
			// 	.then(res=>{
			// 		console.log(res);
			// 	})
			// 	.catch(err=>{
			// 		console.log(err);
			// 	});
			// let pages = getCurrentPages();
			// let route = 'pages/my/index';
			// console.log(this.$mp.page);
			// if(pages.length >= 2 && pages[pages.length - 2].route != 'pages/my/auth'){
			// 	route = pages[pages.length - 2].route;
			// }
			// uni.redirectTo({
			// 	url:'/'.concat(route),
			// 	success(res) {
			// 		console.log(res);
			// 	},
			// 	fail(err) {
			// 		console.log(err);
			// 	}
			// })
			
			// uni.navigateBack({
			// 	delta:1
			// })
		},
		methods: {
			auth() {
				// 注册（跳转授权页）
				// 登录（同上）
				// 失效（同上）
				let that = this;
				let pages = getCurrentPages();
				let route = 'pages/my/my';
				if(pages.length >= 2 && pages[pages.length - 2].route != 'pages/my/auth'){
					route = pages[pages.length - 2].__page__.fullPath/* .fullPath */;
				}
				route = encodeURIComponent(route);
				that.$http.httpReq({url:'api/login/login',token:'',data:{auth:true,target:route}})
					.then(res=>{
						let data = res.data.data;
						// #ifdef H5
						window.location.href = data.url;
						// #endif
						// #ifdef APP-PLUS
						plus.runtime.openURL(data.url);
						// #endif
					})
					.catch(err=>{
						console.log(err);
					})
				
				// console.log('授权跳转',route);
			},
			// 获取请求参数
			getRequest() {
			    var url = window.location.href; //获取url中"?"符后的字串
			    var str = null;
			    var strs = null;
			    var theRequest = new Object();
			    if (url.indexOf("?") != -1) {
			        str = url.substr(url.indexOf("?")+1);
			        strs = str.split("&");
			        for (var i = 0; i < strs.length; i++) {
			            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
			        }
			    }
			    return theRequest;
			}
		}
	}
</script>

<style lang="scss">
	.container{
		overflow-y: hidden;
		.middle{
			.list{
				margin-top: 200rpx;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				.logo{
					width: 350rpx;
					height: 350rpx;
					image{
						width: 100%;
						height: 100%;
					}
				}
				.name{
					padding-top: 20rpx;
					height: 40rpx;
					line-height: 40rpx;
				}
			}
		}
		.bottom{
			.btn{
				width: 80%;
				margin: 80rpx auto;
				border-radius: 10rpx;
				color: #FFFFFF;
				background: #06bf12;
				&:active{
					color: grey;	
				}
			}
		}
	}
</style>
